package fina2.upload;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.naming.InitialContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import fina2.dcs.common.DCSUIUtil;
import fina2.dcs.context.ContextHelper;
import fina2.dcs.upload.UploadedFile;
import fina2.dcs.upload.UploadedFileLocal;

@SuppressWarnings("serial")
public class DownloadServlet extends HttpServlet {

	private Log log = Logging.getLog(getClass());

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		resp.setContentType("application/octet-stream");

		if (req.getParameter("id") == null) {
			return;
		}
		ArrayList<Long> idsList = null;
		UploadedFileLocal ufl = null;
		ServletOutputStream out = resp.getOutputStream();
		List<UploadedFile> files = new ArrayList<UploadedFile>();
		try {
			idsList = DCSUIUtil.parseString(req.getParameter("id"), ",");

			InitialContext jndi = ContextHelper.getInitialContext();
			ufl = ContextHelper.injectUploadedFileLocal(jndi);
			files = ufl.loadFilesToDownload(idsList);

			if (idsList.size() > 1) {
				ZipOutputStream zipOutputStream = new ZipOutputStream(out);

				resp.setHeader("Content-Disposition", "inline; filename=xls_files.zip;");

				for (UploadedFile f : files) {
					zipOutputStream.putNextEntry(new ZipEntry(f.getFileName()));
					zipOutputStream.write(f.getUploadedFile());
				}
				zipOutputStream.closeEntry();
				zipOutputStream.close();
				out.close();
				files.clear();
			} else {
				for (UploadedFile f : files) {
					File file = new File(f.getFileName());
					file.createNewFile();

					ServletContext servContext = getServletConfig().getServletContext();
					String mimeType = servContext.getMimeType(file.getName());
					resp.setContentType((mimeType != null) ? mimeType : "application/octet-stream");
					resp.setContentLength((int) file.length());
					resp.setHeader("Content-Disposition", "attachment; filename=" + file.getName());

					out.write(f.getUploadedFile());

					file.delete();
				}
			}
		} catch (Exception ex) {
			log.error("Can't Download File id - '" + req.getParameter("id") + "'" + ex.getMessage(), ex);
			resp.sendRedirect("/fina-dcs-wp/security/downloadError.html");
		} finally {
			idsList.clear();
			out.close();
			files.clear();
		}
	}

}
